package de.poketradeshop.ui.controller;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import de.poketradeshop.database.DatabaseService;
import de.poketradeshop.ui.model.Customer;
import de.poketradeshop.ui.model.CustomerSession;
import de.poketradeshop.ui.service.MailService;

@Named
@SessionScoped
public class CustomerController implements Serializable {

	@Inject
	private Customer customer;

	@Inject
	private DatabaseService databaseService;

	@Inject
	private CustomerSession customerSession;

	public CustomerController() {

	}

	/**
	 * Register customer in database
	 * @return String
	 */
	public String registerCustomer() {
		try {

			// Check if email exists in database
			boolean exists = databaseService.emailExists(customer.getEmail());

			if (exists) {
				// If email exists, return a error message, that email already
				// is saved in database
				FacesContext.getCurrentInstance().addMessage(
						"register:email",
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fehler",
								"Diese Email wird bereits verwendet!"));
				return "";
			} else {
				// If email not exists in database, the customer with its data
				// is saved in the database
				databaseService.createCustomer(customer.getEmail(), customer.getPassword(),
						customer.getCity(), customer.getFriendcode(), customer.getName(),
						customer.getPostcode(), customer.getStreet(), customer.getTelephone(),
						customer.getBankDepositor(), customer.getBankCode(),
						customer.getBankAccountNo());
				// A confirmation mail will be send to the customer-mail, if the
				// registration is done.
				MailService.sendMessageRegistration(customer.getEmail());
				return "registrationComplete.xhtml";
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * Updates a customer in database that already exists 
	 * @return
	 */
	public void updateCustomer() {
		try {
			// Get the current customer id
			int id = customerSession.getCustomerId();

			// Check if customer edits password or not
			if (customer.getPassword() == "") {
				// Do update in database without password
				databaseService.updateCustomer(id, customer.getEmail(), customer.getCity(),
						customer.getFriendcode(), customer.getName(), customer.getPostcode(),
						customer.getStreet(), customer.getTelephone(), customer.getBankDepositor(),
						customer.getBankCode(), customer.getBankAccountNo());
				
				customerSession.setCustomerName(customer.getName());

				// Send confirmation that profile is updated
				MailService.sendMessageUpdate(customer.getEmail());

			} else {
				// Check if oldPassword matches with database to confirm your identity
				boolean passwordMatches = databaseService.passwordMatches(customer.getEmail(),
						customer.getOldPassword());

				if (passwordMatches) {
					// Do update with password
					databaseService.updateCustomer(id, customer.getEmail(), customer.getPassword(),
							customer.getCity(), customer.getFriendcode(), customer.getName(),
							customer.getPostcode(), customer.getStreet(), customer.getTelephone(),
							customer.getBankDepositor(), customer.getBankCode(),
							customer.getBankAccountNo());
					
					customerSession.setCustomerName(customer.getName());

					// Send confirmation that profile is updated
					MailService.sendMessageUpdate(customer.getEmail());
				} else {
					// return error message, that password not matches
					FacesContext.getCurrentInstance().addMessage(
							"editCustomer:oldPassword",
							new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fehler",
									"Bitte überprüfen Sie Ihr eingegebenes Passwort!"));
				}

			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * Checks customer id and returns all relevant data from customer
	 * @return String
	 */
	public String getAllCustomerData() {

		// Set CustomerData searching by actual session
		Customer customer = databaseService.getCustomerByID(customerSession.getCustomerId());

		// Set Data
		this.customer.setEmail(customer.getEmail());
		this.customer.setPassword(customer.getPassword());
		this.customer.setName(customer.getName());
		this.customer.setCity(customer.getCity());
		this.customer.setPostcode(customer.getPostcode());
		this.customer.setStreet(customer.getStreet());
		this.customer.setBankAccountNo(customer.getBankAccountNo());
		this.customer.setBankCode(customer.getBankCode());
		this.customer.setBankDepositor(customer.getBankDepositor());
		this.customer.setFriendcode(customer.getFriendcode());
		this.customer.setTelephone(customer.getTelephone());

		return "";
	}

}
